package it.unibo.mumu;

import it.unibo.mumu.R;
import it.unibo.mumu.entity.Planet;
import it.unibo.mumu.util.SingletonSupport;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoPlayerActivity extends Activity {
	private VideoView mVideoView;
	private MediaController mediaCtrl;
	private Planet planet;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_player);
		
		int idRealUniverse = SingletonSupport.getInstance().getRealUniverseID();
		int idRealPlanet = SingletonSupport.getInstance().getRealPlanetID();
		
		planet = SingletonSupport.getInstance().getUniverses().get(idRealUniverse).getPlanets().get(idRealPlanet);
		//String tmp = getFilesDir() + "/" + planet.getMultimediaHappy().getVideo().getVideoPath();
		
		mVideoView = (VideoView) findViewById(R.id.videoContainer);
		
		if(SingletonSupport.getInstance().isHappiness()){
			// video Happy
			mVideoView.setVideoURI(Uri.parse(getFilesDir() + "/" + planet.getMultimediaHappy().getVideo().getVideoPath()));
		}else{
			// video Sad
			mVideoView.setVideoURI(Uri.parse(getFilesDir() + "/" + planet.getMultimediaSad().getVideo().getVideoPath()));
		}
		
		
		
		mediaCtrl = new MediaController(this);
		mVideoView.setMediaController(mediaCtrl);
		mVideoView.requestFocus();
		
		mVideoView.showContextMenu();
		mVideoView.start();	
		mediaCtrl.show(0); // dopo 3 secondi si nasconde
		
	}

	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE, R.string.action_back);
        item.setIcon( R.drawable.ic_action_info);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId() == R.id.action_flip){
			
	        	Intent toInfo = new Intent(VideoPlayerActivity.this, InfoActivity.class);
	        	toInfo.putExtra("TITLE", "Informazioni sul cont. multimediale");
	        	if(SingletonSupport.getInstance().isHappiness()){
	        		toInfo.putExtra("TEXT", planet.getMultimediaHappy().getVideo().getVideoTitle());	
	        	}else{
	        		toInfo.putExtra("TEXT", planet.getMultimediaSad().getVideo().getVideoTitle());
	        	}
	        	
//				toPlanet.putExtra("PLANET_ID", ps.getIdPlanet());
				startActivity(toInfo);
	        
        }
        return super.onOptionsItemSelected(item);
    }
	
	@Override
	public void onBackPressed() {
		Intent toPlanetDetail = new Intent(VideoPlayerActivity.this,
				PlanetDetailActivity.class);
		System.gc();
		finish();
		startActivity(toPlanetDetail);
	}

}
